home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PsL Monthly 1993 December
/
PSL Monthly Shareware CD-ROM (December 1993).iso
/
prgmming
/
dos
/
c
/
wi_libc.exe
/
WI.DOC
< prev
next >
Wrap
Text File
|
1991-05-26
|
26KB
|
688 lines
-------------------------[ WI.LIB ]----------for OS/2--------------------------
WI.LIB - Wilkes Index Library
Copyright (c) 1989 by Roger Wilkes
Member: Association of Shareware Professionals (ASP)
Cost of this Software $40; Gov./Ed. $30
Wilkes Software, inc.
Memphis, TN 38134
or Register on BBS at (901)377-5608
-------------------------------------------------------------------------------
There are a total of six (6) function calls available with this library.
1) IX_add - to add keys to an index file
2) IX_del - to delete keys from an index file
3) IX_find_first - to find the first key which meets comparison criteria
4) IX_find_last - to find the last key which meets comparison criteria
5) IX_find_next - to find the next key
6) IX_find_prev - to find the previous key
-------------------------------------------------------------------------------
You must insure that "WI.LIB" is available to the linker at link time.
This library is meant to be linked with Microsoft large (-AL) or huge (-AH)
memory models.
Place WI.LIB in your LIB directory and place INDEX.H in your INCLUDE
directory.
The maximum char array key length is 127. All data types other than this
one can use the "data_type" defines (see INDEX.H). The character array
data type is formed by anding the length and 0x80.
-------------------------------------------------------------------------------
1) Adding a new key to the index file:
Indexing/Reindexing a file:
int IX_add (long file_pos,
char *key_addr,
unsigned char data_type,
int file_handle);
file_pos - is the piece of data which will accompany the key. This
information will be returned to you when you use the
IX_find_xxxxx function calls. If you are working with
fixed length records this can be a record length. If you
use this field to contain a record number, you will have
to do multiplications to get it to a byte position within
the file you are indexing. You may simply use this field
to contain the byte position of the record being indexed.
key_addr - contains the address of either
1) the key data
2) a KEY_STRUCT (see INDEX.H) structure containing up to
10 key parts. The order of importance of the key
fields is from lowest index to highest (i.e.
KEY_COMPONENT.key[0] to KEY_COMPONENT.key[9]).
data_type - specifies that the "key_value" above is either key data
or a KEY_STRUCT structure (see INDEX.H). If data_type
designates key data, the length (in bytes) is also known.
RETURNS: One of the function returns shown in INDEX.H
OK where no errors were detected
IX_IO_ERR
IX_ERR
INV_PARAM
INV_NUM_KEYS
EXAMPLE: if we have a file with the following record layout which we
wish to index on the last_name
-------------------------------------------------------------------------------
/* using the Microsoft "C" Optimizing compiler */
#include <index.h> /* after placing in include dir */
#include <fcntl.h>
#include <sys\types.h>
#include <sys\stat.h>
#include <io.h>
#include <stdlib.h>
#include <stdio.h>
#define TRUE 1
#define FALSE 0
int nad_file;
int index_file;
int num_bytes;
int err;
long file_pos = 0L;
struct {
char first_name [15];
char last_name [15];
char addr [30];
char city [20];
char state [2];
char zip [9];
} name_and_addr;
#include <os2.h>
#define _NOEXIST_FAIL 0x00
#define _NOEXIST_CREATE 0x10
#define _EXIST_FAIL 0x00
#define _EXIST_OPEN 0x01
#define _EXIST_CREATE 0x02
#define Close(f) (errno = DosClose (f))
#define Lseek(f,p,o,l) (errno = DosChgFilePtr (f, p, o, &l))
#define Read(f,b,l,s) (errno = DosRead (f, b, l, &s))
#define Write(f,b,l,s) (errno = DosWrite (f, b, l, &s))
int act_cod;
main ()
{
if (DosOpen ("nameaddr.fil", &nad_file, &act_cod, 0L, 0,
_NOEXIST_CREATE | _EXIST_OPEN, 0x112, 0L))
{
/* do something about the error */
}
if (DosOpen ("nameaddr.inx", &index_file, &act_cod, 0L, 0,
_NOEXIST_CREATE | _EXIST_OPEN, 0x112, 0L))
{
/* do something about the error */
}
while (TRUE)
{
Read (nad_file, (char *)&name_and_addr,
sizeof(name_and_addr), num_bytes);
if (num_bytes == 0) /* end of file */
break;
if (num_bytes != sizeof(name_and_addr))
{
/* take care of short read */
}
err = IX_add (file_pos, name_and_addr.last_name,
sizeof(name_and_addr.last_name)+0x80, index_file);
/* 0x80 is simply the high order bit
of the data_type. For all other data
types you can use the #define names
(see INDEX.H) */
switch (err)
{
case OK:
printf ("NAME: %15.15s indexed\n",
name_and_addr.last_name);
break;
case IX_IO_ERR:
printf ("IO Error on \"nameaddr.inx\" file\n");
break;
case IX_ERR:
printf ("Index File is corrupted\n");
break;
case INV_PARAM:
printf ("Function Call Parameters don't match\n");
printf (" information already in \"nameaddr.inx\"\n");
break;
case INV_NUM_KEYS:
printf ("The number of key fields does not match\n");
printf (" information already in \"nameaddr.inx\"\n");
break;
default: break;
}
if (err)
break;
file_pos += sizeof(name_and_addr);
}
}
-------------------------------------------------------------------------------
2) Deleting an index entry from an index file:
int IX_del (char *key_addr,
long file_pos,
unsigned char data_type,
int file_handle);
key_addr - contains the address of either
1) the key data
2) a KEY_STRUCT (see INDEX.H) structure containing up to
10 key parts. The order of importance of the key
fields is from lowest index to highest (i.e.
KEY_COMPONENT.key[0] to KEY_COMPONENT.key[9]).
file_pos - is the piece of data which will accompany the key. This
information will be returned to you when you use the
IX_find_xxxxx function calls. If you are working with
fixed length records this can be a record length. If you
use this field to contain a record number, you will have
to do multiplications to get it to a byte position within
the file you are indexing. You may simply use this field
to contain the byte position of the record being indexed.
The file position field is required in the delete to
differentiate between duplicate keys and must be the same
as used during IX_add.
data_type - specifies that the "key_value" above is either key data
or a KEY_STRUCT structure (see INDEX.H). If data_type